
/** OPTIONS, LIBREFS **/
OPTIONS MPRINT NOFMTERR PS=60 LS=80 sasautos =macro;
FILENAME macro "/disk/agedisk2/medicare.work/newhouse-DUA19577/jeff/programs/macro";

libname rg '/disk/agedisk2/medicare.work/newhouse-DUA19577/jeff/programs/measures/tables/regression/rru';
libname res '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results';
libname wk '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results';
libname wk9 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2009';
libname wk8 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2008';
libname wk7 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2007';
libname wk6 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2006';
libname wk5 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2005';
libname wk4 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2004';
libname wk3 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/results/2003';
libname ma '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/MA_files';
libname perm7 '/disk/agebulk2/medicare.work/newhouse-DUA19577/jeff/DATA/rru/cardiac/2007';

x "/bin/rm /disk/agedisk2/medicare.work/newhouse-DUA19577/jeff/programs/measures/tables/regression/allcov*.sas7bdat";
x "/bin/rm /disk/agedisk2/medicare.work/newhouse-DUA19577/jeff/programs/measures/tables/regression/allf*.sas7bdat";


***PLANS DEBUT YEAR;
data deb;
 set res.plans_debut (keep = org_id debut);
proc sort;
 by org_id;

***TAX STATUS, AFFILIATION;
data pln;
 set res.hmoplans (keep = org_id taxstatus07 affiliation07);
 rename taxstatus07=taxstatus
 affiliation07 = affiliation;
proc sort;
 by org_id;

***PLAN SIZE;
data size;
 set res.plan_size (keep = org_id pats07);
 rename pats07 = size;
proc sort;
 by org_id;


data card;
 set perm7.cardiac_results_new;

data sm_c;
 length org_id $ 5;
 set card;
 where var in ("ip_co", "em_co", "surg_");
proc sql;
 create table sm_c as select org_id,
 sum(ffs_plan_mean) as ffs_plan_mean,
 sum(hmo_plan_mean) as hmo_plan_mean from sm_c
 group by org_id;

***PLAN LEVEL MEASURE EXCLUSIONS;
data drop;
 set wk.selectplans (keep = org_id
 exc_cmc_07 exc_bcs_07
 exc_cdc1_07 exc_cdc3_07 exc_cdc4_07);
proc sort;
 by org_id;


***MATCHED MEASURE RESULTS;

data a;
 set wk.matched_qual_results_plan (keep = org_id ffs_07 hmo_07 meas);
 where meas in ("ldl");
proc sort;
 by org_id;


data mg (drop =  exc_cdc4_07 exc_cdc3_07 exc_cdc1_07
 exc_cmc_07 exc_bcs_07);
 merge a (in=aa) drop sm_c (in=r) deb pln size;
 by org_id;
 if aa;
 if r;

 if meas = "ldl" and exc_cmc_07 = 1 then delete;

proc sort;
 by meas;

proc means noprint;
 var ffs_07 hmo_07;
 class meas;
 output out=temp;

data sd (rename = (ffs_07=ffs_sd hmo_07=hmo_sd));
 set temp;
 where meas ~= "" and _stat_ = "STD";
 keep meas ffs_07 hmo_07;
proc sort;
 by meas;

data mn (rename = (ffs_07=ffs_mn hmo_07=hmo_mn));
 set temp;
 where meas ~= "" and _stat_ = "MEAN";
 keep meas ffs_07 hmo_07;
proc sort;
 by meas;


data mg;
 merge mg sd mn;
 by meas;

z_ffs = (ffs_07-ffs_mn)/ffs_sd;
z_hmo = (hmo_07-hmo_mn)/hmo_sd;
 if meas = "ldl";
 if size > 100;
 if hmo_07 ~= 0 and ffs_07 ~= 0;
 if hmo_plan_mean = . or ffs_plan_mean = . then delete;
 if affiliation ~= "";

proc freq;
 tables debut affiliation taxstatus;

data mg2;
 set mg; 
 file "card_comp.txt";
 if _n_ = 1 then do;
 put " org_id, " @;
 put " sumcosthmo, " @;
 put " sumcostffs, " @;
 put " debut, " @;
 put " allcomphmo, " @;
 put " allcompffs, " @;
 put " affiliation, " @;
 put " taxstatus, " @;
 put " size";
end;

 put org_id "," hmo_plan_mean "," ffs_plan_mean "," debut ","
 hmo_07 "," ffs_07 "," affiliation "," taxstatus "," size;




 diff_cost = hmo_plan_mean - ffs_plan_mean;
 diff_meas = hmo_07 - ffs_07;

label diff_cost = "HMO cost - FFS cost"
 diff_meas = "HMO diab comp - FFS disb_comp";


proc means median;
 var hmo_07 ffs_07 diff_meas diff_cost;
 












